package cn.gok.demo18.controller;

import cn.gok.demo18.entity.User;
import cn.gok.demo18.service.UserService;
import com.sun.javafx.scene.layout.region.SliceSequenceConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author HP
 * @Date 14:47 2022/7/18
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/selectAll")
    public List<User> selectAllUser() {
        List<User> userList = userService.selectAllUser();
        System.out.println(userList);
        return userList;
    }

    @RequestMapping("/selectById")
    public User selectByUser(@RequestParam int id) {
        User user = userService.selectByUser(id);
        System.out.println(user);
        return user;
    }

    @RequestMapping("/add")
    public int addUser(@RequestBody User user) {
        int rows = userService.addUser(user);
        if (rows != 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("新增失败");
        }
        return rows;

    }

    @RequestMapping("/update")
    public int updateUser(@RequestBody User user) {
        int rows = userService.updateUser(user);
        if (rows != 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
        return rows;
    }

    @RequestMapping("/delete")
    public int deleteUser(@RequestParam int id) {
        int rows = userService.deleteUser(id);
        if (rows != 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        return rows;
    }

    @RequestMapping("/login")
    public User login(@RequestBody User user) {
        String rows= userService.login(user);
        if (rows!=null) {
            System.out.println("登入成功");
        } else {
            System.out.println("账号或密码错误");
        }
        return user;
    }
//    @RequestMapping("/register")
//    public int register(@RequestBody User user){
//        int rows= userService.register(user);
//        if (rows != 0) {
//            System.out.println("注册成功");
//        } else {
//            System.out.println("注册成功");
//        }
//        return rows;
//    }
    @RequestMapping("/register")
    public String register(@RequestBody User user){
        User user1 = userService.selectByNameAndPassword(user.getName(),null);
        if (user1!=null){
            return "用户名已存在";
        }else {
            userService.addUser(user);
            return "注册成功";
        }
    }
}


